#ifndef MISC_H_
#define MISC_H_

#include <stddef.h>

#define CASE_ENUM_STR(e) \
    case e: \
        return #e

#if __SIZEOF_SIZE_T__ != __SIZEOF_INT__
#define FMT_OF_SIZE "lu"
#else
#define FMT_OF_SIZE "u"
#endif

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))

#ifndef container_of
#define container_of(ptr, type, member) ((type *)( (char *)(ptr) - offsetof(type,member) ))
#endif

#define ADDRSTRLEN (INET6_ADDRSTRLEN + 2 + 6)

#define is_power_of_2(n) ((n) != 0 && (((n) & ((n) - 1)) == 0))
int ilog2(size_t v);
#define roundup_pow_of_2(n) ((n == 1) ? 1 : (1UL << (ilog2((n) - 1) + 1)))
#define rounddown_pow_of_2(n) (1UL << ilog2(n))


const char *sockaddr_ntop(const void *addr, char *buf, size_t buflen);

int create_listen_socket(const char *host, const char *srv, int family, int sockType);
int get_socket_errno(int sock);

#endif /* MISC_H_ */
